//
//  NavigationSub2Demo.m
//  Exercise
//
//  Created by zhouxiaobo on 15/11/14.
//  Copyright © 2015年 mapath. All rights reserved.
//

#import "NavigationSub2Demo.h"

static int myIndex = 0;

@implementation NavigationSub2Demo

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    self.title = [NSString stringWithFormat:@"导航计数%d",myIndex];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(screenWidth/2 - 40, 120, 80, 30);
    btn1.backgroundColor = [UIColor redColor];
    [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [btn1 setTitle:@"Push" forState:UIControlStateNormal];
    [btn1 setTitle:@"Push" forState:UIControlStateHighlighted];
    [btn1 addTarget:self action:@selector(goNext) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(screenWidth/2 - 40, 180, 80, 30);
    btn2.backgroundColor = [UIColor redColor];
    [btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [btn2 setTitle:@"Pop" forState:UIControlStateNormal];
    [btn2 setTitle:@"Pop" forState:UIControlStateHighlighted];
    [btn2 addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn3.frame = CGRectMake(screenWidth/2 - 40, 240, 80, 30);
    btn3.backgroundColor = [UIColor redColor];
    [btn3 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [btn3 setTitle:@"Root" forState:UIControlStateNormal];
    [btn3 setTitle:@"Root" forState:UIControlStateHighlighted];
    [btn3 addTarget:self action:@selector(goRoot) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
    
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn4.frame = CGRectMake(screenWidth/2 - 40, 300, 80, 30);
    btn4.backgroundColor = [UIColor redColor];
    [btn4 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [btn4 setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [btn4 setTitle:@"Index" forState:UIControlStateNormal];
    [btn4 setTitle:@"Index" forState:UIControlStateHighlighted];
    [btn4 addTarget:self action:@selector(goIndex:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn4];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goNext {
    NavigationSub2Demo *navigationSub2Demo = [[NavigationSub2Demo alloc]init];
    myIndex ++ ;
    navigationSub2Demo.titleNumber = myIndex;
    [self.navigationController pushViewController:navigationSub2Demo animated:YES];
}

- (void)goRoot {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void) goIndex : (UIButton *) button{
    //从所有层数的堆栈数组中，获取第index层
    UIViewController *controler = [[self.navigationController viewControllers] objectAtIndex:1];
    [self.navigationController popToViewController:controler animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    NSLog(@"Controller%d销毁了",self.titleNumber);
    //[super dealloc];
}

@end


